﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace PCR.Logic
{
    using PCR.Base;
        
    /// <summary>
    /// Virus type manager class, contains all the operation of the Virus type
    /// </summary>
    internal class VirusTypeManager : IVirusTypeManager
    {
        IDatabase _database = null;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="database">Database interface<param>
        public VirusTypeManager(IDatabase database)
        {
            System.Diagnostics.Debug.Assert(database != null);
            this._database = database;
        }

        /// <summary>
        /// Add a virus type
        /// </summary>
        /// <param name="item">Virus type to add</param>
        public void Add(IVirusType item)
        {
            Debug.Assert(item != null);
            this._database.VirusTypes.Append(item);
        }

        /// <summary>
        /// Add a list of virus type
        /// </summary>
        /// <param name="items">Virus type list to add</param>
        public void Add(IList<IVirusType> items)
        {
            Debug.Assert(items != null);
            this._database.VirusTypes.Append(items);
        }

        /// <summary>
        /// Remove a virus type
        /// </summary>
        /// <param name="item">Virus type to remove</param>
        public void Remove(IVirusType item)
        {
            Debug.Assert(item != null);
            this._database.VirusTypes.Delete(item);
        }
        
        /// <summary>
        /// Remove the virus type by ID
        /// </summary>
        /// <param name="id">The virus type ID to remove</param>
        public void RemoveByID(Guid id)
        {
            this._database.VirusTypes.DeleteByID(id);
        }

        /// <summary>
        /// Remove all the virus type
        /// </summary>
        public void RemoveAll()
        {
            this._database.VirusTypes.DeleteAll();
        }

        /// <summary>
        /// Modify the specified virus type
        /// </summary>
        /// <param name="item">Virus type to modify</param>
        public void Modify(IVirusType item)
        {
            Debug.Assert(item != null);
            this._database.VirusTypes.Update(item);
        }

        /// <summary>
        /// Modify the virus type by ID
        /// </summary>
        /// <param name="id">The virus type ID to modify</param>
        /// <param name="item">The virus type to modify</param>
        public void Modify(Guid id, IVirusType item)
        {
            Debug.Assert(item != null);
            this._database.VirusTypes.UpdateByID(id, item);
        }

        /// <summary>
        /// Get the virus type by ID
        /// </summary>
        /// <param name="id">The virus type ID to find</param>
        /// <returns>NULL if not found</returns>
        public IVirusType GetByID(Guid id)
        {
            return this._database.VirusTypes.SelectByID(id);   
        }

        /// <summary>
        /// Get all the virus types
        /// </summary>
        public IList<IVirusType> List 
        {
            get 
            { 
                return this._database.VirusTypes.SelectAll(); 
            }
        }

        /// <summary>
        /// Search the object by specified filter
        /// </summary>
        /// <param name="filter">Search filter, contains all the options to search</param>
        public IList<IVirusType> Search(ISearchFilter<IVirusType> filter)
        {
            return this._database.VirusTypes.Search(filter);
        }

        /// <summary>
        /// Get or set the base cell count, all the virus CT calculation
        /// will base on this same value
        /// </summary>
        public UInt32 BaseCellCount
        {
            get 
            { 
                if (this._baseCellCount == 0)
                {
                    // Load it from file
                    this._baseCellCount = this._database.VirusTypes.BaseCellCount;
                }

                return this._baseCellCount;
            }
            
            set 
            { 
                this._database.VirusTypes.BaseCellCount = value;
                
                // Cache it for fast access
                this._baseCellCount = value;
            }
        }
        UInt32 _baseCellCount = 0;
        
        /// <summary>
        /// Get or set the virus valve value
        /// if the virus Exponent > VirusSafetyExponent, it will be "阳性"
        /// else it will be "阴性"
        /// </summary>
        public double VirusSafetyExponent
        {
            get 
            {
                // First load
                if (this._virusSafetyExponent == 0.0)
                {
                    this._virusSafetyExponent = this._database.VirusTypes.VirusSafetyExponent; 
                }

                return this._virusSafetyExponent;
            }
            
            set 
            {
                this._database.VirusTypes.VirusSafetyExponent = value;

                // Cache it for fast access
                this._virusSafetyExponent = value;
            }
        }

        double _virusSafetyExponent = 0.0;
    }
}
